package com.rc.saas.tenant.service.tenant;

import com.rc.saas.tenant.mapper.tenant.TenantOrganizationMapper;
import com.rc.saas.tenant.model.tenant.TenantOrganization;
import com.rc.saas.tenant.model.tenant.TenantOrganizationExample;
import com.rc.saas.tenant.utils.BuildTreeUtil;
import com.rc.saas.tenant.vo.tenant.TreeModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sven on 2018-05-17
 */
@Service
public class TenantOrgServiceImpl implements TenantOrgService {
    @Autowired
    private TenantOrganizationMapper tenantOrganizationMapper;

    public TenantOrganizationMapper getMapper() {
        return tenantOrganizationMapper;
    }

    @Override
    public TreeModel<TenantOrganization> buildOrgTree() {
        List<TreeModel<TenantOrganization>> trees = new ArrayList<TreeModel<TenantOrganization>>();

        TenantOrganizationExample example = new TenantOrganizationExample();
        //example.createCriteria().andTenantCodeEqualTo(tenantCode);
        example.setOrderByClause("parent_id, tenant_org_id");
        List<TenantOrganization> list = tenantOrganizationMapper.selectByExample(example);

        for (TenantOrganization org : list) {
            TreeModel<TenantOrganization> tree = new TreeModel<TenantOrganization>();
            tree.setId(org.getTenantOrgId().toString());
            tree.setParentId(org.getParentId().toString());
            tree.setText(org.getOrgName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);

            trees.add(tree);
        }

        // 默认顶级菜单为０，根据数据库实际情况调整
        //TreeVo<CmsOrganization> t = BuildTreeUtil.build(trees);
        TreeModel<TenantOrganization> t = BuildTreeUtil.buildWholeTree(trees);

        return t;
    }

    @Override
    public TreeModel<TenantOrganization> buildOrgUserTree() {
        List<TreeModel<TenantOrganization>> trees = new ArrayList<TreeModel<TenantOrganization>>();

        List<TenantOrganization> list = tenantOrganizationMapper.listOrganizationUser();

        //设置所有节点属性
        for (TenantOrganization org : list) {
            TreeModel<TenantOrganization> tree = new TreeModel<TenantOrganization>();
            tree.setId(org.getTenantOrgId().toString());
            tree.setParentId(org.getParentId().toString());
            tree.setText(org.getOrgName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);

            if (org.getTenantUserId() != null) {
                tree.setIcon("fa fa-user");
            }
            trees.add(tree);
        }

        // 构建树,默认顶级菜单为０，根据数据库实际情况调整
        TreeModel<TenantOrganization> tree = BuildTreeUtil.buildWholeTree(trees);

        return tree;
    }

    //public TreeVo<CmsOrganization> getDept(String deptId) {
    //    TreeVo<CmsOrganization> deptNode = getDeptNode(deptId);
    //    if (deptNode == null) {
    //        return null;
    //    }
    //
    //    List<TreeVo<CmsOrganization>> childNode = getChildNode(deptId);
    //    for (TreeVo<CmsOrganization> child : childNode) {
    //        TreeVo<CmsOrganization> node = getDept(child.getId());
    //        deptNode.getChildren().add(node);
    //        deptNode.setChildren(true);
    //    }
    //    return deptNode;
    //}
    //
    //private TreeVo<CmsOrganization> getDeptNode(String deptId) {
    //    //Map<String, Object> query = new HashMap<>();
    //    //query.put("deptId", deptId);
    //    //List<CmsOrganization> deptList = sysDeptMapper.list(query);
    //    CmsOrganization sysDept = tenantOrganizationMapper.selectByPrimaryKey(Long.valueOf(deptId));
    //    if (sysDept != null) {
    //        //CmsOrganization sysDept = deptList.get(0);
    //        TreeVo<CmsOrganization> tree = getDeptTree(sysDept);
    //        return tree;
    //    } else {
    //        return null;
    //    }
    //}
    //
    //private List<TreeVo<CmsOrganization>> getChildNode(String deptId) {
    //    //Map<String, Object> query = new HashMap<>();
    //    //query.put("parentId", deptId);
    //
    //    List<TreeVo<CmsOrganization>> trees = new ArrayList<>();
    //
    //    CmsOrganizationExample example = new CmsOrganizationExample();
    //    example.createCriteria().andParentIdEqualTo(Long.valueOf(deptId));
    //    List<CmsOrganization> sysDepts = tenantOrganizationMapper.selectByExample(example);
    //
    //    //List<CmsOrganization> sysDepts = sysDeptMapper.list(query);
    //    for (CmsOrganization sysDept : sysDepts) {
    //        TreeVo<CmsOrganization> tree = getDeptTree(sysDept);
    //        trees.add(tree);
    //    }
    //    return trees;
    //}
    //
    //private TreeVo<CmsOrganization> getDeptTree(CmsOrganization sysDept) {
    //    TreeVo<CmsOrganization> tree = new TreeVo<>();
    //    tree.setId(sysDept.getOrgId().toString());
    //    tree.setParentId(sysDept.getParentId().toString());
    //    tree.setText(sysDept.getOrgName());
    //    Map<String, Object> state = new HashMap<>(16);
    //    state.put("opened", true);
    //    tree.setState(state);
    //    if (!("0".equals(tree.getParentId()))) {
    //        tree.setHasParent(true);
    //    }
    //    return tree;
    //}

}
